﻿using System;
using System.Drawing;
using System.Net;
using System.Text;
using Domain;
using Domain.Entity;

namespace Spider.Spiders.Workers
{
    internal partial class QqSprider : Spider
    {
        internal static string VerifyCode { get; set; }
        internal static Image VerifyCodeImage { get; set; }
        internal static bool AutoWritVerifyCode { get; set; }//自动识别验证码
        internal static int AutoWritVerifyCodeErrorTimes { get; set; }//自动识别验证码错误次数，单个账号
        protected int _currentAcountVerifyCodeErrorTimes = 2;//当前QQ账号自动识别验证码错误次数，单个账号
        private static bool _hasvisitedDestinationUrl = false;//是否当前账号已经访问过指定地址
        private bool _zoneOpened = true;//默认假设当前目标QQ空间开放
        internal static string Param { get; set; }
        internal static CookieContainer Cookie { get; set; }
        internal static bool EnableTimeLineVisite { get; set; }//启用时光轴留痕
        internal static string VisiteDestinationQQ { get; set; }//启用访问自己指定QQ
        internal static bool ApplyVisiteZone { get; set; }//申请访问空间
        internal static bool ZoneAddFriend { get; set; }//申请空间加好友
        internal static bool ShuoShuoSayGood { get; set; }//空间说说点赞
        internal static bool IndexSayGood { get; set; }//空间首页点赞
        internal static bool ZoneMessageBorderLiveMessage { get; set; }//空间留言板留言
        internal static bool EnableZoneLogsSayGood { get; set; }//空间日志点赞
        internal static bool CheckZoneLogsAvaliable { get; set; }//检查空间日志是否可访问
        public override void StartToWork(ToDoData data)
        {
            try
            {
                string finnalurl =  GetFinalUrl(data.PageUrl, data.DataSource);
                PageData pageData = new PageData();
                switch (data.DataSource)
                {
                    case PageDataSource.QQVisitor:
                        pageData = GetQQVisitorResult(finnalurl);
                        break;
                    case PageDataSource.QQSeed:
                        pageData = GetQQZoneIndexResult(finnalurl);
                        break;
                    case PageDataSource.QQNumberForZone:
                        pageData = VisiteQqZone(finnalurl);
                        break;
                }
                if (pageData != null)
                {
                    pageData.PageUrl = data.PageUrl;
                    PageRepository.AddPage(pageData);
                }
            }
            catch (Exception ex)
            {
                SpiderCaptain.GetInstance().StopToWork(ex.ToString());
                CustomsLog.WriteExceptionErrorLog("QQ爬虫工作异常", ex);
            }

        }
        /// <summary>
        /// 获取QQ访问者数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private PageData GetQQVisitorResult(string url)
        {
            var ret = GetWebContentWithCookie(url, Cookie);
            PageData pageData = new PageData();
            pageData.Data = ret;
            pageData.DataSource = PageDataSource.QQVisitor;
            pageData.Date = DateTime.Now;
            return pageData;
        }
        /// <summary>
        /// 访问QQ空间
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        private PageData VisiteQqZone(string qq)
        {
            QQAcount acount =  DealLoginQQZone();
            if (EnableTimeLineVisite)
                VisiteZoneTimeLine(qq,acount.QQ);
            if(ZoneAddFriend)
                ApplyZoneAddFriendByMessageBord(qq);
            if(ZoneMessageBorderLiveMessage)
                ZoneBorderLiveMessage(qq);
            if (ShuoShuoSayGood)
                ZoneShuoShuoSayGood(qq);
            if (IndexSayGood)
               ZoneIndexSayGood(qq);
            if(EnableZoneLogsSayGood)
               ZoneLogsSayGood(qq);
            if (ApplyVisiteZone)
                ApplyZoneVisite(qq);
            //访问指定地址
            if (!_hasvisitedDestinationUrl && !string.IsNullOrWhiteSpace(VisiteDestinationQQ) && (acount.MaxVisiteTimes - 1) == acount.VisitedTimes)
            {
                //CustomsLog.WriteInfoLog("指定QQ被访问", "账号：" + acount.QQ + ",已经访问了指定QQ：" + VisiteDestinationQQ);
                _hasvisitedDestinationUrl = true;
                if (EnableTimeLineVisite)
                    VisiteZoneTimeLine(VisiteDestinationQQ, acount.QQ);
                if (ApplyVisiteZone)
                    ApplyZoneVisite(VisiteDestinationQQ);
                if (ZoneAddFriend)
                    ApplyZoneAddFriendByMessageBord(qq);
                if (ZoneMessageBorderLiveMessage)
                    ZoneBorderLiveMessage(qq);
                if (ShuoShuoSayGood)
                    ZoneShuoShuoSayGood(VisiteDestinationQQ);
                if (IndexSayGood)
                    ZoneIndexSayGood(VisiteDestinationQQ);
                if (EnableZoneLogsSayGood)
                    ZoneLogsSayGood(qq);
            }
           
            return null;
        }
        /// <summary>
        /// 获取QQ首页HTML源代码,处理探测空间是否可以访问
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private PageData GetQQZoneIndexResult(string url)
        {
            var ret = GetSourceHtml(url,Encoding.UTF8);
            PageData pageData = new PageData();
            pageData.Data = ret;
            pageData.DataSource = PageDataSource.QQZoneIndex;
            pageData.Date = DateTime.Now;
            return pageData;
        }
    }
}
